<?php


	/**
	 * Shortcode: column
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_column_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'size' => '1-2',
				'last' => 'false'				
				), $atts ) );

		return ( $last == 'true') ? '<div class="cv-column cv-column-' . $size . ' cv-column-last">' . do_shortcode( $content ) . '</div><div class="cv-spacer"></div>' : '<div class="cv-column cv-column-' . $size . '">' . do_shortcode( $content ) . '</div>';
	}

	/**
	 * Shortcode: list
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_list_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'style' => 'star'
				), $atts ) );
		
		return '<div class="cv-list cv-list-style-' . $style . '">' . do_shortcode( $content ) . '</div>';
	}

	/**
	 * Shortcode: quote
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_quote_shortcode( $atts, $content = null ) {
		extract(shortcode_atts(array(
			'name' => '',
			'sitename' => '',
			'siteurl' => '',
			'avatar' => ''
		), $atts));
		
		$output = '<div class="cv-quote"><div class="cv-quote-inner"><p class="cv-quote-text clearfix">';
		
		if($avatar)
			$output .= '<img class="cv-avatar" src="'.$avatar.'" alt="" />';
		
		$output .= do_shortcode($content). '</p><p class="cv-quote-from">';
	   
		if($name)
			$output .= '~ <span class="cv-fn">'.$name.'</span>';
		
		if($siteurl && $sitename)
			$output .= ', <a class="cv-su" href="'.$siteurl.'"><span class="cv-sitename">'.$sitename.'</span></a>';
		elseif($sitename)
			$output .= ', <span class="cv-sn">'.$sitename.'</span>';

	   $output .= '</p></div></div>';
	   
	   return $output;
	}

	/**
	 * Shortcode: toggle
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_toggle_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'title' => __( 'Click To Open', 'shortcodes-cv' ),
				'color' => ''
				), $atts ) );
			
		return '<div class="cv-toggle cv-toggle-open cv-toggle-' . $color . '"><div class="cv-toggle-title">' . $title . '</div><div class="cv-toggle-content">' . do_shortcode( $content ) . '</div></div>';
	}

	/**
	 * Shortcode: button
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_button_shortcode( $atts, $content = null ) {
		extract(shortcode_atts(array(
		'color' => '',
		'url' => '#',
		'value' => 'button'
		), $atts));
		
		if($color)
			$class = 'cv-button cv-button-'.$color;
		else
			$class = 'cv-button';
		
		$output = '<a href="'.$url.'" class="'.$class.'"><span class="cv-button-text">'.$value.'</span><span class="cv-button-right"></span></a>';
		
		return $output;
	}
	
	/**
	 * Shortcode: tabs
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_tabs_shortcode( $atts, $content ) {
		extract( shortcode_atts( array(
				'style' => 'horizontal'
				), $atts ) );

		$GLOBALS['tab_count'] = 0;

		do_shortcode( $content );

		if ( is_array( $GLOBALS['tabs'] ) ) {
			foreach ( $GLOBALS['tabs'] as $tab ) {
				$tabs[] = '<span>' . $tab['title'] . '</span>';
				$panes[] = '<div class="cv-tabs-pane">' . $tab['content'] . '</div>';
			}
			$return = '<div class="cv-tabs cv-tabs-style-' . $style . '"><div class="cv-tabs-nav">' . implode( '', $tabs ) . '</div><div class="cv-tabs-panes">' . implode( "\n", $panes ) . '</div><div class="cv-spacer"></div></div>';
		}
		return $return;
	}

	/**
	 * Shortcode: tab
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_tab_shortcode( $atts, $content ) {
		extract( shortcode_atts( array( 'title' => 'Tab %d' ), $atts ) );
		$x = $GLOBALS['tab_count'];
		$GLOBALS['tabs'][$x] = array( 'title' => sprintf( $title, $GLOBALS['tab_count'] ), 'content' => do_shortcode( $content ) );
		$GLOBALS['tab_count']++;
	}
	
	/**
	 * Shortcode: spacer
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_spacer_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'size' => 0
				), $atts ) );

		return '<div class="cv-spacer" style="height:' . $size . 'px"></div>';
	}
	
	/**
	 * Shortcode: gmap
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_gmap_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'width' => 600,
				'height' => 400,
				'address' => 'Singapore, Marina bay'
				), $atts ) );

		return '<iframe width="' . $width . '" height="' . $height . '" src="http://maps.google.com/maps?q=' . urlencode( $address ) . '&amp;output=embed" class="cv-gmap"></iframe>';
	}
	
	/**
	 * Shortcode: contact
	 *
	 * @param array $atts Shortcode attributes
	 * @param string $content
	 * @return string Output html
	 */
	function cv_contact_shortcode( $atts, $content = null ) {
		extract( shortcode_atts( array(
				'mailto' => 'yourname@yourmail.com',				
				'namelabel' => 'You Name',
				'emaillabel' => 'Email',
				'subjectlabel' => 'Subject',
				'contentlabel' => 'Please type here'				
				), $atts ) );
		
		$form_action    = get_permalink();
		$author_default = $_COOKIE['comment_author_'.COOKIEHASH];
		$email_default  = $_COOKIE['comment_author_email_'.COOKIEHASH];
		
		if ( ($_SESSION['contact_form_success']) ) {
		$contact_form_success = '<p style="color: green">Thank your for Your Feedback.</p>';
		unset($_SESSION['contact_form_success']);
		}
		
$markup = <<<EOT

<div class="cv-contactform">

	{$contact_form_success}
     
   <form onsubmit="return validateForm(this);" action="{$form_action}" method="post" enctype="multipart/form-data" style="text-align: left">
   
   <p><input type="text" name="author" id="cv-author" value="{$author_default}" size="22" /> <label for="author">{$namelabel} *</label></p>
   <p><input type="text" name="email" id="cv-email" value="{$email_default}" size="22" /> <label for="email">{$emaillabel} *</label></p>
   <p><input type="text" name="subject" id="cv-subject" value="" size="22" /> <label for="subject">{$subjectlabel} *</label></p>
   <p><textarea name="message" id="cv-message" cols="100%" rows="10">{$contentlabel}</textarea></p>  
   <p><input name="send" type="submit" id="cv-send" value="Send" /></p>
   
   <input type="hidden" name="contact_form_submitted" value="1">
   <input type="hidden" name="mailto" value="{$mailto}">
   
   </form>
   
</div>

EOT;
		
		return $markup;		
	}
	
	function cv_has_my_shortcode( $posts ) {
        if ( empty($posts) )
            return $posts;

        $shortcode_found = false;
        foreach ($posts as $post) {

            if ( !( stripos($post->post_content, '[contact') === false ) || !( stripos($post->post_content, '[cv_contact') === false ) ) {
                $shortcode_found = true;
                break;
            }
        }
        if ( $shortcode_found ) {
			add_action('wp_head', 'cv_contact_form_js');
           
        }
        return $posts;
    }
	add_action('the_posts', 'cv_has_my_shortcode');
	
	
	function cv_contact_form_js() { ?>
		<script type="text/javascript">
		function validateForm(form) {

			var errors = '';
			var regexpEmail = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
				
			if (!form.author.value) errors += "Warning 1 : Name box empty.\n";
			if (!regexpEmail.test(form.email.value)) errors += "Warning 2 : Email box empty.\n";
			if (!form.subject.value) errors += "Warning 3 : Subject.\n";

			if (errors != '') {
				alert(errors);
				return false;
			}			
		return true;			
		}
		</script>
	<?php }
	
	
	add_action('init', 'cv_contact_form_process');
	function cv_contact_form_process() {

		session_start();

		 if ( !isset($_POST['contact_form_submitted']) ) return;

		 $author  = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
		 $email   = ( isset($_POST['email']) )   ? trim(strip_tags($_POST['email'])) : null;
		 $subject = ( isset($_POST['subject']) ) ? trim(strip_tags($_POST['subject'])) : null;
		 $message = ( isset($_POST['message']) ) ? trim(strip_tags($_POST['message'])) : null;
		 
		 $mailto   = ( isset($_POST['mailto']) ) ? trim(strip_tags($_POST['mailto'])) : null;

		 if ( $author == '' ) wp_die('Error 1: Add your Name Please.'); 
		 if ( !is_email($email) ) wp_die('Error 2: Type your Email Please.');
		 if ( $subject == '' ) wp_die('Error 3: Add a Subject First.');
		 
		 //we will add e-mail sending support here soon
		 
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		$mail_to_send = new PHPMailer();

		$mail_to_send->FromName = $author;
		$mail_to_send->From     = $email;
		$mail_to_send->Subject  = $subject;
		$mail_to_send->Body     = $message;

		$mail_to_send->AddReplyTo($email);
		$mail_to_send->AddAddress($mailto); //contact form destination e-mail		

		if ( !$mail_to_send->Send() ) wp_die('Mail Sending Failed. Error info: ' . $mail_to_send->ErrorInfo .' Please check your mail server settings and contact form destination e-mail address');

		$_SESSION['contact_form_success'] = 1;
		 
		header('Location: ' . $_SERVER['HTTP_REFERER']);
		exit();
	}	 
	
	
?>